﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
 
namespace eFatura.Utility.Grid
{
    public class GridManager
    {
        #region Events & Handlers
        public event bulkInsertClicked BulkInsert;
        public delegate void bulkInsertClicked(GridControl gridControl);
        #endregion

        public GridUtility gridUtility;
        private GridMenu gridMenu;
        public GridMenu GridMenu
        {
            get { return gridMenu; }
            set { gridMenu = value; }
        }
        private GridView gridView;

        public static Dictionary<int, GridManager> Items = new Dictionary<int, GridManager>();

        public GridManager(GridView gridView)
        {
            this.GridView = gridView;
            this.gridMenu = new GridMenu(this);
            this.gridUtility = new GridUtility(this);

            Grid.KeyPress += new KeyPressEventHandler(Grid_KeyPress);
            Grid.ProcessGridKey += new KeyEventHandler(Grid_ProcessGridKey);            
            GridView.PopupMenuShowing += new PopupMenuShowingEventHandler(GridView_PopupMenuShowing);
            GridView.RowCellStyle += new RowCellStyleEventHandler(gridView_RowCellStyle);
            GridView.InvalidValueException += new InvalidValueExceptionEventHandler(GridView_InvalidValueException);
        }

        void GridView_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            gridMenu.CurrentColumnName = "";
            gridMenu.CurrentBandName = "";

            if (e.MenuType == GridMenuType.Row)
            {
                e.Menu = gridMenu.RowMenu;
            }
            else if (e.MenuType == GridMenuType.Column)
            {
                if (e.HitInfo.GetType() == typeof(DevExpress.XtraGrid.Views.BandedGrid.ViewInfo.BandedGridHitInfo))
                {
                    DevExpress.XtraGrid.Views.BandedGrid.ViewInfo.BandedGridHitInfo bhi = (DevExpress.XtraGrid.Views.BandedGrid.ViewInfo.BandedGridHitInfo)e.HitInfo;
                    if (bhi.Column == null && bhi.Band != null)
                    {
                        gridMenu.CurrentBandName = bhi.Band.Name;
                    }
                }
                else if (e.HitInfo.GetType() == typeof(DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo))
                {
                    GridColumn gc = e.HitInfo.Column;
                    if (gc != null)
                    {
                        GridMenu.CurrentColumnName = gc.FieldName;
                    }
                }

                foreach (DevExpress.Utils.Menu.DXMenuItem mi in gridMenu.ColumnMenu.Items)
                {
                    e.Menu.Items.Add(mi);
                }
            }
        }

        void GridView_InvalidValueException(object sender, InvalidValueExceptionEventArgs e)
        {
            if (!this.gridUtility.ShowValidationException)
            {
                throw e.Exception;
            }
        }

        void Grid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 22 || e.KeyChar == 7)
            {
                e.Handled = true;
            }
        }

        public GridView GridView
        {
            get { return gridView; }
            set { gridView = value; }
        }

        public GridControl Grid
        {
            get { return gridView.GridControl; }

        }

        public static GridManager GetManager(GridView gridView)
        {
            GridManager gridManager = null;

            bool exists = Items.TryGetValue(gridView.GetHashCode(), out gridManager);

            if (!exists)
            {
                GridManager gm = new GridManager(gridView);
                GridManager.Items.Add(gridView.GetHashCode(), gm);
                Items.TryGetValue(gridView.GetHashCode(), out gridManager);
            }

            return gridManager;
        }

        static void gridview_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.Column.FieldName == "Scan" && e.RowHandle > -1)
            {
                GridView view = sender as GridView;
                var productId = view.GetDataRow(e.RowHandle)["ProductId"];
                if (productId != DBNull.Value)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    //XForm.ShowProductPhoto(((int)productId));
                    Cursor.Current = Cursors.Default;
                }
            }
        }


        static void gridview_MouseMove(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;

            GridHitInfo info = view.CalcHitInfo(new Point(e.X, e.Y));


            if (info.InRowCell && info.Column.FieldName == "Scan" && info.RowHandle > -1)
            {
                var productId = view.GetDataRow(info.RowHandle)["ProductId"];
                if (productId != DBNull.Value)
                    view.GridControl.Cursor = Cursors.Hand;
            }
            else
                view.GridControl.Cursor = Cursors.Default;
        }

        public static void gvProduct_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.ListSourceRowIndex > -1 && e.Column.FieldName == "Scan")
                if (e.IsGetData)
                {
                    //int ProductId = 0;
                    //int.TryParse(((DataRowView)e.Row)["ProductId"].ToString(), out ProductId);
                    //if (ProductId > 0)
                    //{
                    //    AppDS.V_KumasRow ar = RepositoryManager.dsi.V_Kumas.FindByProductId(ProductId);

                    //    if (ar != null)
                    //        if (!ar.IsPhotoMikroNull())
                    //            e.Value = ar.PhotoMikro;
                    //}
                }
        }

        private void gridView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle == DevExpress.XtraGrid.GridControl.AutoFilterRowHandle)
            {
                e.Appearance.BackColor = System.Drawing.Color.LightGoldenrodYellow;
            }
        }

        private void Grid_ProcessGridKey(object sender, KeyEventArgs e)
        {
            GridControl gc = (GridControl)sender;
            int FocusedRowHandle = (gc.MainView as ColumnView).FocusedRowHandle;
            GridColumn focusedColumn = (gc.MainView as ColumnView).FocusedColumn;

            e.Handled = true;

            if ((e.KeyData == (Keys.Control | Keys.V)))
            {
                e.Handled = gridUtility.PasteFromClipboard();
            }
            else if ((e.KeyData == (Keys.Control | Keys.Delete)))
            {
                gridUtility.DeleteRow();
            }
            else if ((e.KeyData == Keys.Delete))
            {
                if (GridView.ActiveEditor == null || !GridView.ActiveEditor.IsEditorActive)
                    gridUtility.DeleteValue();
                else
                    e.Handled = false;
            }
            else if (e.KeyData == (Keys.Control | Keys.G) && GridView.Name != "gvColumnFocuser")
            {
                gridUtility.SutunaGit();
            }
            else if (e.KeyData == (Keys.Control | Keys.D0))
            {
                if (GridView.ActiveEditor != null)
                    GridView.ActiveEditor.EditValue = DBNull.Value;
                else
                    e.Handled = false;
            }
            else if ((e.KeyData == (Keys.Control | Keys.T)) & !GridUtility.IsReadOnly(GridView))
            {
                BulkInsert(Grid);
            }
            else if ((e.KeyData == (Keys.Control | Keys.Up)
                  || e.KeyData == (Keys.Control | Keys.Up | Keys.Shift)) && !gc.MainView.IsEditing)
            {
                gridUtility.MoveUp(e.Shift);
            }
            else if ((e.KeyData == (Keys.Control | Keys.Down)
                  || e.KeyData == (Keys.Control | Keys.Down | Keys.Shift)) && !gc.MainView.IsEditing)
            {
                gridUtility.MoveDown(e.Shift);
            }
            else if ((e.KeyData == (Keys.Control | Keys.Left)
                  || e.KeyData == (Keys.Control | Keys.Left | Keys.Shift)) && !gc.MainView.IsEditing)
            {
                gridUtility.MoveLeft(e.Shift);
            }
            else if ((e.KeyData == (Keys.Control | Keys.Right)
                  || e.KeyData == (Keys.Control | Keys.Right | Keys.Shift)) && !gc.MainView.IsEditing)
            {
                gridUtility.MoveRight(e.Shift);
            }
            else
            {
                e.Handled = false;
            }
        }
    }
}